var util = require('../../utils/util.js');
var app = getApp();

Page({
  data: {
    task: {},
    creating: false,
    button: {
      txt: '修改'
    },
    modalHidden: true
  },
  // 提示
  handleWarning() {
      wx.showModal({
        title: '温馨提示',
        content: '开始时间不能大于截止时间',
        showCancel: false
      })
  },
  // 设置任务名称
  bindKeyInput: function (e) {
    this.setData({
      'task.name': e.detail.value
    });
  },
  // 设置备注
  bindContextInput: function (e) {
    this.setData({
      'task.remark': e.detail.value
    });
  },
 
  // 设置开始打卡时间
 setSignTime: function (e) {
  var that = this;
  var task = this.data.task;
  if (task.signEarlyTime >= e.detail.value){
    this.handleWarning();
    return
  }
  that.setData({
    'task.signTime': e.detail.value
  });
},
  // 设置截止打卡时间
  setSignEarlyTime: function (e) {
    var that = this;
    var task = this.data.task;
    if (task.signTime <= e.detail.value){
      this.handleWarning();
      return
    }
    that.setData({
      'task.signEarlyTime': e.detail.value
    });
  },
  
  // 设置开始日期
  startDateChange: function (e) {
    this.setData({
      'task.startDay': e.detail.value
    })
  },

  // 设置结束日期
  endDateChange: function (e) {
    this.setData({
      'task.endDay': e.detail.value
    })
    
  },

  // 设置重复日
  changeMonday: function (e) {
    var state = this.data.task.repeat.monday;
    this.setData({
      'task.repeat.monday': (state == 1 ? 0 : 1) 
    });
  },
  changeTuesday: function (e) {
    var state = this.data.task.repeat.tuesday;
    this.setData({
      'task.repeat.tuesday': (state == 1 ? 0 : 1) 
    });
  },
  changeWednesday: function (e) {
    var state = this.data.task.repeat.wednesday;
    this.setData({
      'task.repeat.wednesday': (state == 1 ? 0 : 1) 
    });
  },
  changeThursday: function (e) {
    var state = this.data.task.repeat.thursday;
    this.setData({
      'task.repeat.thursday': (state == 1 ? 0 : 1) 
    });
  },
  changeFriday: function (e) {
    var state = this.data.task.repeat.friday;
    this.setData({
      'task.repeat.friday': (state == 1 ? 0 : 1) 
    });
  },
  changeSaturday: function (e) {
    var state = this.data.task.repeat.saturday;
    this.setData({
      'task.repeat.saturday': (state == 1 ? 0 : 1) 
    });
  },
  changeSunday: function (e) {
    var state = this.data.task.repeat.sunday;
    this.setData({
      'task.repeat.sunday': (state == 1 ? 0 : 1) 
    });
  },

  // 隐藏提示弹层
  modalChange: function (e) {
    this.setData({
      modalHidden: true
    })
  },

  // 修改任务
  createTask: function () {
    var that = this;
    var task = this.data.task;
    const db = wx.cloud.database()
    wx.showToast({
      title: '修改中',
      icon: 'loading',
      duration: 5000
    });
      console.log(task)
			db.collection('clock_project').doc(task._id).update({
				data: {
					name: task.name, // 打卡名称
						signEarlyTime: task.signEarlyTime, // 开始时间
						signTime: task.signTime, // 截止时间
						startDay: task.startDay, // 开始日期
						endDay: task.endDay, // 截止日期
						repeat: task.repeat,
						remark: task.remark,// 备注
						imageId: task.imageId // 图片信息ID
				},
				success: res => {
					// 在返回结果
					wx.showToast({
						title: '修改成功',
					})
					setTimeout(function () {
						wx.navigateBack({
							
						})
					}, 800)
					// console.log('[数据库] [新增记录] 成功，记录: ', res)
				},
				fail: err => {
					wx.showToast({
						icon: 'none',
						title: '修改失败'
					})
					console.error('[数据库] [新增记录] 失败：', err)
				}
			})

  },

  // 提交、检验
  bindSubmit: function (e) {
    var that = this;
    var task = this.data.task;
    var creating = this.data.creating;
    
    if (task.name == '') {
      this.setData({
        modalHidden: false
      });
    } else {
      if (!creating) {
        this.setData({
          'creating': true
        });
        that.createTask();
      }
    }
  },
  
  onShow: function () {
    // 恢复新建按钮状态
    this.setData({
      'creating': false
    });
  },

  onHide: function () {
  },

  // 初始化设置
  onLoad: function (option) {
		const eventChannel = this.getOpenerEventChannel()
		var that = this;
		// 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
		eventChannel.on('acceptDataFromOpenerPage', function (data) {
			that.setData({
				task: data.data
		 })
		})

  }
})